package com.duowei.diandan.print;


import android.text.TextUtils;
import android.util.Log;

import com.duowei.diandan.app.OrderList;
import com.duowei.diandan.bean.Payment;
import com.duowei.diandan.bean.QrCodePay;
import com.duowei.diandan.constant.PayType;
import com.duowei.diandan.db.AppDatabase;
import com.duowei.diandan.db.entry.Jgsz;
import com.duowei.diandan.db.entry.Wmlsb;
import com.duowei.diandan.db.entry.Wmlsbjb;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import am.util.printer.PrintDataMaker;
import am.util.printer.PrinterWriter;
import am.util.printer.PrinterWriter58mm;
import am.util.printer.PrinterWriter80mm;

public class PayTicketPrintDataMaker implements PrintDataMaker {
    private static final String TAG = "PayTicketPrintDataMaker";

    private int mType;

    private Wmlsbjb mWmlsbjb;
    private List<Wmlsb> mWmlsbList;
    private boolean mIsCommit;
    private List<Payment> mPaymentList;

    public PayTicketPrintDataMaker(Wmlsbjb wmlsbjb, List<Wmlsb> wmlsbList, boolean isCommit) {
        mWmlsbjb = wmlsbjb;
        mWmlsbList = wmlsbList;
        mIsCommit = isCommit;
        mPaymentList = OrderList.getInstance().getPaymentList();
    }

    @Override
    public List<byte[]> getPrintData(int type) {
        mType = type;
        ArrayList<byte[]> data = new ArrayList<>();

        try {
            PrinterWriter printer;
            if (type == PrinterWriter58mm.TYPE_58) {
                printer = new PrinterWriter58mm();
            } else {
                printer = new PrinterWriter80mm();
            }

            printer.print("\n");
            printer.setEmphasizedOn();
            printer.setLineHeight(75);
            data.add(printer.getDataAndReset());

            Jgsz jgsz = AppDatabase.getDatabase().jgszDao().findOne();
            if (jgsz != null && !TextUtils.isEmpty(jgsz.getBmmc())) {
                printer.setFontSize(1);
                printer.setAlignCenter();
                printer.print(jgsz.getBmmc() + "\n\n");
                printer.setFontSize(0);
                printer.setAlignLeft();
            }

            printer.print("桌号: " + mWmlsbjb.getZh() + "\n");
            printer.print("账单号: " + mWmlsbjb.getWmdbh() + "\n");
            printer.print("日期: " + mWmlsbjb.getJysj().toString("yyyy-MM-dd HH:mm:ss") + "\n");

            printer.print(getLine());
            printer.printInOneLine("单品名称", String.format(Locale.CHINA, "%s     %s", "数量", "金额"), 0);
            printer.print("\n");
            for (Wmlsb w : mWmlsbList) {
                printer.printInOneLine(w.getXmmc(), String.format(Locale.CHINA, "X%.0f%s   ￥%.2f\n", w.getSl(), w.getDw(), w.getXj()), 0);
                if (!TextUtils.isEmpty(w.getPz())) {
                    printer.print(w.getPz() + "\n");
                }
            }

            printer.print(getLine());

            if (mIsCommit) {
                printer.print(String.format(Locale.CHINA, "消费合计: ￥%.2f\n", mWmlsbjb.getYs()));
            } else {
                printer.print(String.format(Locale.CHINA, "消费总额: ￥%.2f\n", mWmlsbjb.getYsje()));
                printer.print(String.format(Locale.CHINA, "折扣金额: ￥%.2f\n", mWmlsbjb.getZk()));
                printer.print(String.format(Locale.CHINA, "应收金额: ￥%.2f\n", mWmlsbjb.getYs()));

                if (mPaymentList != null && mPaymentList.size() > 0) {
                    printer.print(getLine());
                    for (Payment p : mPaymentList) {
                        if (p.payType == PayType.STORED_VALUE_TYPE) {
                            printer.print(String.format(Locale.CHINA, "%s  ￥%.2f\n", p.title, p.payMoney));
                        } else if (p.payType == PayType.INTEGRAL_TYPE) {
                            printer.print(p.title + "  " + p.jfValue + "\n");
                        } else if (p.payType == PayType.COUPON_TYPE) {
                            printer.print(p.title + "  " + p.ticketNum + "张\n");
                        }
                    }
                    printer.print(getLine());
                }

                QrCodePay qrCodePay = OrderList.getInstance().getQrCodePay();
                if (qrCodePay != null) {
                    printer.print(String.format(Locale.CHINA, "%s  ￥%.2f\n", qrCodePay.getTitle(), qrCodePay.getPrice()));
                }


                printer.setAlignCenter();
                printer.print("\n谢谢惠顾\n\n");
                printer.setAlignLeft();
            }

            printer.setEmphasizedOff();
            printer.feedPaperCutPartial();
            data.add(printer.getDataAndReset());
            return data;
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
            return new ArrayList<>();
        }
    }

    private String getLine() {
        if (mType == PrinterWriter58mm.TYPE_58) {
            return "-------------------------------\n";
        } else {
            return "------------------------------------------------\n";
        }
    }
}
